package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.DiagnosisListDetailService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.masterData.DiagnosisListDetailDelDto;
import com.ccys.db.dto.masterData.DiagnosisListDetailPageQueryDto;
import com.ccys.db.dto.masterData.DiagnosisListDetailSaveDto;
import com.ccys.db.entity.TblDiagnosisListDetail;
import com.ccys.db.mapper.TblDiagnosisListDetailMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.masterData.DiagnosisListDetailPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 诊断集合详情实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 17:44
 **/

@Service
public class DiagnosisListDetailServiceImpl extends BaseServiceImpl<TblDiagnosisListDetail> implements DiagnosisListDetailService {

    @Resource
    private TblDiagnosisListDetailMapper tblDiagnosisListDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveDiagnosisListDetail(DiagnosisListDetailSaveDto dto) {
        //需要校验当前集合是否存在同一个诊断 - 未删除的
        if (ObjectUtil.isNotEmpty(dto.getListId()) && ObjectUtil.isNotEmpty(dto.getDiagnosisId())) {
            //准备查询条件
            Example example = new Example(TblDiagnosisListDetail.class);
            example.createCriteria()
                    .andEqualTo("listId", dto.getListId())
                    .andEqualTo("diagnosisId", dto.getDiagnosisId())
                    .andEqualTo("delFlag", 0);
            //如果ID存在 - 则属于修改 - 需要排除自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                example.createCriteria().andNotEqualTo("id", dto.getId());
            }
            //查询并判断
            if (ObjectUtil.isNotEmpty(tblDiagnosisListDetailMapper.selectOneByExample(example))) {
                return ResponseResult.error("This diagnosis already exists in the current set!");
            }
        }
        //调用新增/修改 - 并直接返回是否成功
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteDiagnosisListDetail(DiagnosisListDetailDelDto dto) {
        //校验参数
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备参数进行软删除
        TblDiagnosisListDetail detail = new TblDiagnosisListDetail();
        detail.setId(dto.getId());
        detail.setDelFlag(1);
        //进行操作并返回是否成功
        if (tblDiagnosisListDetailMapper.updateByPrimaryKeySelective(detail) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult<PageInfo<DiagnosisListDetailPageQueryVo>, Object> findPageDiagnosisListDetail(DiagnosisListDetailPageQueryDto dto) {
        //校验参数
        if (ObjectUtil.isEmpty(dto.getListId())) {
            return ResponseResult.error("Parameter Error!");
        }
        PageDto.startPage();
        PageInfo<DiagnosisListDetailPageQueryVo> info = new PageInfo<>(tblDiagnosisListDetailMapper.findPageDiagnosisListDetail(dto));
        return ResponseResult.success(info);
    }
}
